#!/bin/bash 
set -e


TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage4}
SRC=${SRC:-/sources}

name=db
version=5.3.28

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-$version.tar.gz -C $TMP

{ time \
   {

    cd $TMP/$name-$version
    
	sed -i 's/\(__atomic_compare_exchange\)/\1_db/' src/dbinc/atomic.h

	cd build_unix
	../dist/configure --prefix=/usr      \
	                  --enable-compat185 \
	                  --enable-dbm       \
	                  --disable-static   \
	                  --enable-cxx
	make

	make docdir=/usr/share/doc/$name-$version install

	chown -v -R root:root                        \
	      /usr/bin/db_*                          \
	      /usr/include/db{,_185,_cxx}.h          \
	      /usr/lib/libdb*.{so,la}                \
	      /usr/share/doc/$name-$version

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
